package com.gdth.listener.impl;

import com.gdth.listener.OnTelePhoneListener;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/** 
 * @author Arvin
 * @version 
 * 电话状态类
 */
public class PhoneStateListenerImpl extends PhoneStateListener { 
	/** 标识拨号 **/
	private boolean isPhoneing = false;
	
	/** 标识来电 **/
	private boolean isRinging = false;

	/** 电话监听扩展接口 **/
	private OnTelePhoneListener mOnTelePhoneListener;
	
	public PhoneStateListenerImpl(OnTelePhoneListener mOnTelePhoneListener){
		this.mOnTelePhoneListener = mOnTelePhoneListener;
	}
	
	
	@Override     
	public void onCallStateChanged(int state, String incomingNumber) {     
		super.onCallStateChanged(state, incomingNumber);     

		switch (state) {     
			case TelephonyManager.CALL_STATE_IDLE:
//				接通电话
				if (isPhoneing){
					mOnTelePhoneListener.CALL_STATE_OFFHOOK_DOWN();
					
					isPhoneing = false;
				} else if (isRinging){
					mOnTelePhoneListener.CALL_STATE_OFFHOOK_DOWN();
					isRinging = false;
				}
				else{
					mOnTelePhoneListener.CALL_STATE_IDLE();
				}
				break;     
			case TelephonyManager.CALL_STATE_RINGING:
//				响铃
				if (!isRinging){
					mOnTelePhoneListener.CALL_STATE_RINGING();
					isRinging = true; 
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK: 
//				挂断电话
				if (isRinging){
					mOnTelePhoneListener.CALL_STATE_OFFHOOK();
					isRinging = true;
				} else{
					mOnTelePhoneListener.CALL_STATE_OFFHOOK_CALL();
					isPhoneing = true;
				}
				break;
		}        
	}
}
